package com.grass.cropTradingCenter.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grass.cropTradingCenter.common.OutputException;
import com.grass.cropTradingCenter.mapper.MiniShopMapper;
import com.grass.cropTradingCenter.pojo.entity.MiniShop;
import com.grass.cropTradingCenter.pojo.entity.MiniShopUser;
import com.grass.cropTradingCenter.service.MiniShopService;
import com.grass.cropTradingCenter.service.MiniShopUserService;
import com.grass.cropTradingCenter.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author zipan
* @description 针对表【mini_shop】的数据库操作Service实现
* @createDate 2024-01-23 19:41:54
*/
@Service
public class MiniShopServiceImpl extends ServiceImpl<MiniShopMapper, MiniShop>
    implements MiniShopService{

    @Autowired
    MiniShopUserService miniShopUserService;

    @Override
    public void addOrUpdateShop(MiniShop vo) {
        int shopUserId = SecurityUtils.getShopUserId();
        // 新增
        if(vo.getShopId() == null || vo.getShopId() == 0){
            vo.setShopUserId(shopUserId);
            this.save(vo);
            MiniShopUser miniShopUser = new MiniShopUser();
            miniShopUser.setShopUserId(shopUserId);
            miniShopUser.setShopNum(1);
            miniShopUserService.updateById(miniShopUser);
            return;
        }
        // 更新
        this.updateById(vo);
    }

    @Override
    public MiniShop getShopDetail() {
        int userId = SecurityUtils.getShopUserId();
        LambdaQueryWrapper<MiniShop> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MiniShop::getShopUserId, userId);
        List<MiniShop> list = this.list(queryWrapper);
        if (list.size() == 0) {
            throw new OutputException("你还没有开店铺");
        }
        return list.get(0);
    }

    @Override
    public void updateStartNum(Integer shopId, int num) {
        MiniShop shop = this.getById(shopId);
        shop.setStartNum(shop.getStartNum() + num);
        this.updateById(shop);
    }
}




